from multiprocessing import SimpleQueue
import cv2
import logging
from worker import Worker


logger = logging.getLogger(__name__)


class CameraManager(Worker):
    def __init__(
            self, buffers_image: tuple[SimpleQueue, SimpleQueue, SimpleQueue]) -> None:
        self._buffers_image = buffers_image

    def run_forever(self) -> None:
        cap = cv2.VideoCapture(0)
        while cap.isOpened():
            success, frame = cap.read()
            if not success:
                logger.error('Failed to read from camera.')
                break
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            for buffer_image in self._buffers_image:
                buffer_image.put(frame)
                # A word of caution: things enqueued get pickled
        cap.release()
        logger.info('Camera manager stopped.')
